登录功能完成:
- js:设置return
- html:设置
- form
- input
-
οnclick="return fnLogin()"
- py:
- @app.route设置methods
- GET
- POST
- 读取表单数据
- 查询数据库
- 用户名密码对:
- 记住用户名
- 跳转到首页
- 用户名密码不对:
- 提示相应错误。
- 用户名密码对:
session:
- 从`flask`中导入`session`
- 设置`SECRET_KEY`
- 操作字典一样操作`session`:增加用户名`session['username']=`username
function myLogin() { var oUname = document.getElementById("uname"); var oPass = document.getElementById("upass"); var oError = document.getElementById("error_box"); oError.innerHTML = ""; if (oUname.value.length < 6 || oUname.value.length > 20) { oError.innerHTML = "用户名为6-20位。"; return false; } else if((oUname.value.charCodeAt(0)>=48)&&(oUname.value.charCodeAt(0)<=57)){ oError.innerHTML="首位不能是数字"; return false; } else for(var i=0;i
57)&&(oUname.value.charCodeAt(i)<97||oUname.value.charCodeAt(i)>122)){ oError.innerHTML="用户名只能包括字母和数字"; return false; } } if (oPass.value.length < 6 || oPass.value.length > 20) { oError.innerHTML = "密码为6-20位。"; return false; } return true; // window.alert("登录成功!"); } {% extends 'base.html'%}{% block title %}登录{% endblock %}{% block head %} {% endblock %}{% block main %}
登录
from flask import Flask,redirect,request,url_for,sessionfrom flask import render_templatefrom flask_sqlalchemy import SQLAlchemyimport configapp = Flask(__name__)app.config.from_object(config)db=SQLAlchemy(app)class User(db.Model): __tablename__='user' id=db.Column(db.Integer,primary_key=True,autoincrement=True) username=db.Column(db.String(20),nullable=False) password = db.Column(db.String(20), nullable=False) nickname=db.Column(db.String(20))# db.create_all()#查询# user=User.query.filter(User.username=='mis1234').first()# print(user.id,user.username,user.password)#增加# user=User(username='mis1234',password='111111',nickname='l')# db.session.add(user)# db.session.commit()#增加# user=User(username='luk',password='123456',nickname='lu')# db.session.add(user)# db.session.commit()# 修改# user=User.query.filter(User.username=='mis1234').first()# user.password='12345678'# db.session.commit()## # 删除# user=User.query.filter(User.username=='mis1234').first()# db.session.delete(user)# db.session.commit()@app.route('/')def base(): return render_template('base.html')@app.route('/login/',methods=['GET','POST'])def login(): if request.method == 'GET': return render_template('login.html') else: usern = request.form.get('username') passw = request.form.get('password') user = User.query.filter(User.username == usern).first() if user: if user.password==passw: session['user']=usern return redirect(url_for('shouye')) else: return '密码错误。' else: return '用户名不存在。'@app.route('/register/',methods=['GET','POST'])def register(): if request.method=='GET': return render_template('register.html') else: usern=request.form.get('username') nickn = request.form.get('nickname') passw = request.form.get('password') user=User.query.filter(User.username==usern).first() if user: return '用户名已存在。' else: user1=User(username=usern,password=passw,nickname=nickn) db.session.add(user1) db.session.commit() return redirect(url_for('login'))#重定回向登录页面@app.route('/question/')def question(): return render_template('question.html')@app.route('/shouye/')def shouye(): return render_template('shouye.html')if __name__ == '__main__': app.run(debug=True)
import osSQLALCHEMY_DATABASE_URI='mysql+pymysql://root:@127.0.0.1:3306/mis_db?charset=utf8'SQLALCHEMY_TRACK_MODIFICATIONS=FalseSECRET_KEY=os.urandom(24)